package com.sanrenzu.everybodytv.widget;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

import com.handmark.pulltorefresh.library.PullToRefreshScrollView;


/**
 * Created by Administrator on 2016/8/20 0020.
 * 重写PullToRefreshScrollView
 */
public class VerticalScrollView extends PullToRefreshScrollView {
    private int downX;
    private int downY;
    private int mTouchSlop;
    private OnScrollListener onScrollListener;
    private int lastScrollY;

    private ScrollViewListener scrollViewListener = null;
    ScrollView scrollView ;

    public VerticalScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();//
        scrollView = new ScrollView(context);
    }

    public VerticalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();//
    }


    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

    private Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            int scrollY = VerticalScrollView.this.getScrollY();

            //此时的距离和记录下的距离不相等，在隔5毫秒给handler发消息
            if (lastScrollY != scrollY) {
                lastScrollY = scrollY;
                handler.sendMessageDelayed(handler.obtainMessage(), 5);
            }
            if (onScrollListener != null) {
                onScrollListener.onScroll(scrollY);
            }

        }
    };

    public interface ScrollViewListener {

        void onScrollChanged(VerticalScrollView scrollView, int x, int y, int oldx, int oldy);

    }


    public interface OnScrollListener {
        /**
         * 回调方法：返回MyScrollView滑动的Y方向距离
         */
        public void onScroll(int scrollY);
    }

}
